var RC = {};
RC.home = {};
RC.home.initLogin = function () {
    var c = $("#login-form").submit(function () { return false });
    var b = $.cookie("cookies");
    if (b == 0) {
        $("#cookies").checked = false
    }
    $("#username").val($.cookie("username"));
    c.submit(function () {
        $.ajax({
            type: c.attr("method"),
            url: c.attr("action"),
            data: c.serializeArray(),
            dataType: "json",
            beforeSend: function () {
                $("#login-form :input").attr("disabled", true)
            },
            success: function (d) {
                if (!d.success) {
                    alert(d.msg);
                    $("#login-form :input").removeAttr("disabled");
                    a();
                    return
                }
                var f = $("#cookies").is(":checked");
                $.cookie("cookies", f ? 1 : 0, { expires: 365 });
                $.cookie("username", f ? $("#username").val() : null, { expires: 365 });
               // document.body.innerHTML = "";
                //document.title = L.txt.jump;               
                location.href = "/MainDesktop.aspx"
            } 
        })
    });
    $("#username,#password").hints();
    $("#cookies").checkbox();
    function a() {
        if ($("#username").size() && !$("#username").val()) {
            $("#username").focus();
            return
        }
        $("#password").focus()
    }
    $(a);
    $("#login-form").click(function (d) {
        d.stopPropagation()
    });
    $(document.body).click(function (d) {
        a()
    }
)
};
RC.home.initHome = function () {
    $("button[name]").click(function () {
        if (!confirm(this.title)) {
            return false
        }
        switch (this.name) {
            case "logout":
                location = "/cgi-bin/login.cgi?act=logout&language=" + _LANG;
                break;
            case "reboot":
            case "shutdown":
                $.ajax({
                    type: "GET",
                    url: "/cgi-bin/" + _LANG + "/control.cgi?__mode=control&act=" + this.name,
                    dataType: "json",
                    success: function (a) {
                        if (a.success) { window.close() }
                        else { alert(a.msg) }
                    }
                });
                break;
        } 
    })
};
$.fn.hints = function () {
    return this.each(function () {
        var a = this;
        if (!a.hint) {
            var b = this.nextSibling;
            if (!b) {
                b = document.createElement("div");
                b.innerHTML = a.title;
                b.className = "hint";
                a.parentNode.appendChild(b)
            }
            a.title = "";
            var c = $(b);
            c.click(function () {
                a.focus()
            }).bind("selectstart", function () {
                a.focus();
                return false
            }).toggle(!a.value);
            a.hint = c
        }
        $(a).focus(function () {
            this.hint.toggle(!this.value)
        }).blur(function () {
            this.hint.toggle(!this.value)
        }).keydown(function () {
            this.hint.hide()
        }).keyup(function () {
            this.hint.toggle(!this.value)
        }).bind("drop", function () {
            this.hint.toggle(!this.value)
        })
    }
)
};
$.checkbox = function (c, b) {
    var a = this;
    this.opts = $.extend({ cls: "checkbox", over: "checkbox-over", checked: "checkbox-checked", lable: "checkbox-label" }, b);
    this.el = $(c);
    this.el.wrap('<span class="' + this.opts.cls + '"></span>').hide().click(function () { return false });
    this.checkbox = this.el.parent();
    this.checkbox.click(function () {
        a.el.attr("checked", !a.checkbox.hasClass(a.opts.checked));
        a.init()
    });
    if (c.id) { this.bind($('label[for="' + c.id + '"]').addClass(this.opts.lable)) }
    this.bind(this.checkbox);
    this.init()
};
$.checkbox.prototype = { init: function (a) {
    if (a !== undefined) {
        this.el.attr("checked", a ? true : false)
    }
    this.checkbox.toggleClass(this.opts.checked, this.el.is(":checked"))
},
bind: function (b) {
    var a = this;
    $(b).click(function (c) {
        a.el.attr("checked", !a.checkbox.hasClass(a.opts.checked));
        a.init()
    }).mouseover(function (c) {
        a.checkbox.addClass(a.opts.over);
        c.stopPropagation()
    }).mouseout(function (c) {
        a.checkbox.removeClass(a.opts.over);
        c.stopPropagation()
    })
} 
};
$.fn.checkbox = function (a) { return this.each(function () { checkbox = new $.checkbox(this, a) }) };
$.cookie = function (b, j, n) {
    if (typeof j != "undefined") {
        n = n || {};
        if (j === null) { j = ""; n.expires = -1 }
        var d = "";
        if (n.expires && (typeof n.expires == "number" || n.expires.toUTCString)) {
            var f;
            if (typeof n.expires == "number") {
f=new Date();
f.setTime(f.getTime()+(n.expires*24*60*60*1000))}else{f=n.expires}d="; expires="+f.toUTCString()}var m=n.path?"; path="+n.path:"";var g=n.domain?"; domain="+n.domain:"";var a=n.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),d,m,g,a].join("")}else{var k=null;if(document.cookie&&document.cookie!=""){var l=document.cookie.split(";");for(var h=0;h<l.length;h++){var c=$.trim(l[h]);if(c.substring(0,b.length+1)==(b+"=")){k=decodeURIComponent(c.substring(b.length+1));break}}}return k}};if($.browser.msie&&$.browser.version<=6){try{document.execCommand("BackgroundImageCache",false,true)}catch(e){}};
